import { computed, onUnmounted, ref } from "vue";

export function useCountDown(total: number) {
	const cur = ref(total);
	const formatTime = computed(() => {
		let minites = Math.floor(cur.value / 60);
		let seconds = cur.value % 60;
		return minites.toString() + "分" + seconds.toString() + "秒";
	});

	let timer = setInterval(() => {
		cur.value--;
		// console.log("cur = " + cur);
		if (cur.value <= 0) {
			clearInterval(timer);
		}
	}, 1000);

	onUnmounted(() => {
		timer && clearInterval(timer);
	});
	return formatTime;
}